﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FormChooseFireEtinguisher : System.Windows.Forms.Form
	{
		private void FormChooseFireEtinguisher_Load(object sender, EventArgs e)
		{
			this.InitDataGridView();
			this.rdohand.Checked = true;
			this.rdojibie.Checked = true;
		}

		private void rdocar_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdocar.Checked)
			{
				this.listfe = this.sfdr.GetFireExtinguisherData("FireExtinguisherData", "推车式");
				if (this.rdojibie.Checked)
				{
					this.InitDataByJiBie();
					return;
				}
				this.InitDataAll();
			}
		}

		private void rdojibie_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdojibie.Checked)
			{
				this.InitDataByJiBie();
				return;
			}
			this.InitDataAll();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.dataGridView.SelectedRows.Count != 1)
			{
				base.Visible = true;
				YJKMessageBox.Show("请先选择灭火器方案再进行布置");
				return;
			}
			FormLayFireExtinguisher formLayFireExtinguisher = new FormLayFireExtinguisher(this.doc, this.fireExtinguisherParam);
			formLayFireExtinguisher.Owner = this;
			base.Opacity = 0.0;
			if (formLayFireExtinguisher.ShowDialog() == DialogResult.OK)
			{
				this.fireExtinguisherParam = formLayFireExtinguisher.fireExtinguisherParam;
				this.layTerminalType = formLayFireExtinguisher.layTerminalType;
				base.DialogResult = DialogResult.OK;
				return;
			}
			base.Opacity = 1.0;
		}

		private void btnCancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void dataGridView_SelectionChanged(object sender, EventArgs e)
		{
			if (this.rdohand.Checked)
			{
				this.fireExtinguisherParam.SymbolName = "手提式";
			}
			else
			{
				this.fireExtinguisherParam.SymbolName = "推车式";
			}
			this.fireExtinguisherParam.JiBie = this.dataGridView.CurrentRow.Cells[1].Value.ToString();
			this.fireExtinguisherParam.Count = (int)this.dataGridView.CurrentRow.Cells[2].Value;
			this.fireExtinguisherParam.XingHao = this.dataGridView.CurrentRow.Cells[0].Value.ToString();
		}

		private void rdohand_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdohand.Checked)
			{
				this.listfe = this.sfdr.GetFireExtinguisherData("FireExtinguisherData", "手提式");
				if (this.rdojibie.Checked)
				{
					this.InitDataByJiBie();
					return;
				}
				this.InitDataAll();
			}
		}

		public FormChooseFireEtinguisher(bool isA, double Qe, double Min, Document doc)
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.isA = isA;
			this.Qe = Qe;
			this.Min = Min;
			this.doc = doc;
		}

		private void InitDataGridView()
		{
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.HeaderText = "灭火器型号";
			dataGridViewTextBoxColumn.Width = 198;
			dataGridViewTextBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn.ReadOnly = true;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn2.HeaderText = "灭火器级别";
			dataGridViewTextBoxColumn2.Width = 80;
			dataGridViewTextBoxColumn2.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn2.ReadOnly = false;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn2);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn3.HeaderText = "数量";
			dataGridViewTextBoxColumn3.Width = 80;
			dataGridViewTextBoxColumn3.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn3.ReadOnly = true;
			this.dataGridView.Columns.Add(dataGridViewTextBoxColumn3);
		}

		private void InitDataByJiBie()
		{
			this.dataGridView.Rows.Clear();
			if (this.listfe.Count > 0)
			{
				foreach (FireExtinguisherClass fireExtinguisherClass in this.listfe)
				{
					if (this.isA && fireExtinguisherClass.LevelA != "")
					{
						double num = double.Parse(fireExtinguisherClass.LevelA.Remove(fireExtinguisherClass.LevelA.Count<char>() - 1));
						if (num >= this.Min)
						{
							int num2 = Convert.ToInt32(Math.Ceiling(this.Qe / num));
							object[] values = new object[]
							{
								fireExtinguisherClass.Model + "(" + fireExtinguisherClass.Agent + ")",
								fireExtinguisherClass.LevelA,
								num2
							};
							this.dataGridView.Rows.Add(values);
						}
					}
					else if (!this.isA && fireExtinguisherClass.LevelB.Count<char>() > 1)
					{
						double num3 = double.Parse(fireExtinguisherClass.LevelB.Remove(fireExtinguisherClass.LevelB.Count<char>() - 1));
						if (num3 >= this.Min)
						{
							int num2 = Convert.ToInt32(Math.Ceiling(this.Qe / num3));
							object[] values2 = new object[]
							{
								fireExtinguisherClass.Model + "(" + fireExtinguisherClass.Agent + ")",
								fireExtinguisherClass.LevelB,
								num2
							};
							this.dataGridView.Rows.Add(values2);
						}
					}
				}
			}
		}

		private void InitDataAll()
		{
			this.dataGridView.Rows.Clear();
			if (this.listfe.Count > 0)
			{
				foreach (FireExtinguisherClass fireExtinguisherClass in this.listfe)
				{
					if (this.isA && fireExtinguisherClass.LevelA != "")
					{
						double num = double.Parse(fireExtinguisherClass.LevelA.Remove(fireExtinguisherClass.LevelA.Count<char>() - 1));
						int num2 = Convert.ToInt32(Math.Ceiling(this.Qe / num));
						object[] values = new object[]
						{
							fireExtinguisherClass.Model + "(" + fireExtinguisherClass.Agent + ")",
							fireExtinguisherClass.LevelA,
							num2
						};
						this.dataGridView.Rows.Add(values);
					}
					else if (!this.isA && fireExtinguisherClass.LevelB.Count<char>() > 1)
					{
						double num3 = double.Parse(fireExtinguisherClass.LevelB.Remove(fireExtinguisherClass.LevelB.Count<char>() - 1));
						int num2 = Convert.ToInt32(Math.Ceiling(this.Qe / num3));
						object[] values2 = new object[]
						{
							fireExtinguisherClass.Model + "(" + fireExtinguisherClass.Agent + ")",
							fireExtinguisherClass.LevelB,
							num2
						};
						this.dataGridView.Rows.Add(values2);
					}
				}
			}
		}

		private SpaceFamilyDataReader sfdr = new SpaceFamilyDataReader();

		private List<FireExtinguisherClass> listfe = new List<FireExtinguisherClass>();

		private bool isA;

		private double Qe;

		private double Min;

		public FireExtinguisherParam fireExtinguisherParam = new FireExtinguisherParam();

		private Document doc;

		public int layTerminalType;
	}
}
